package controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import model.*;


import org.apache.struts.Globals;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

import view.LoginActionForm;

public class LoginAction extends Action {

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		LoginActionForm loginform = (LoginActionForm) form;
		String username = loginform.getUsername();
		String password = loginform.getPassword();
		CustomerService cs = new CustomerService();
		CustomerBean bean = cs.login(username, password);
		HttpSession session = request.getSession();
		ActionErrors errors = new ActionErrors();
		if (bean != null) {
			session.setAttribute("user", bean);
			session.setAttribute("pservice", new ProductService());
			return mapping.findForward("success");
		} else {
			errors.add("username", new ActionMessage("login.legalerror"));
			request.setAttribute(Globals.ERROR_KEY , errors);
			return mapping.findForward("error");
		}
	}

}
